package com.smcdd.cdb.dbo.cdb_dbo.ui.fragment;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.event.ShopType;
import com.smcdd.cdb.dbo.cdb_dbo.service.presenter.MenDianListPresenter;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IMenDianListFg;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.MenDianActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.ShopPagerAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseFragmentFragment;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.NoScrollViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/23 14:14
 * 邮箱：zhanghuaiha@gmail.com
 */

public class MenDianListFragment extends BaseFragmentFragment<IMenDianListFg,MenDianListPresenter> implements IMenDianListFg{
    private NoScrollViewPager mPager;
    private TextView list_yet,list_not;
    private List<BaseFragmentFragment> mFragmentList = new ArrayList<>();
    private MenDianActivity activity;

    @Override
    public void initView(View rootView) {
        EventBus.getDefault().register(this);
        mPager = (NoScrollViewPager) rootView.findViewById(R.id.list_viewpager);
        list_yet = (TextView) rootView.findViewById(R.id.list_yet);
        list_not = (TextView) rootView.findViewById(R.id.list_not);
    }

    @Override
    public void initData() {

        mFragmentList.add(FragmentFactory.getInstance().getYetLayMenFragment());
        mFragmentList.add(FragmentFactory.getInstance().getNotLayMenFragment());
        mPager.setAdapter(new ShopPagerAdapter(getActivity().getSupportFragmentManager(),mFragmentList));
        list_yet.setSelected(true);

        activity = (MenDianActivity) getActivity();
        String not = activity.getNot();
        if (not != null) {
            list_yet.setSelected(false);
            list_not.setSelected(true);
            mPager.setCurrentItem(1);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mFragmentList.clear();
        EventBus.getDefault().unregister(this);
    }

    //筛选
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSort(ShopType event){
        mPager.setCurrentItem(0);
    }

    @Override
    public void initListener() {
        list_yet.setOnClickListener(v -> {
            mPager.setCurrentItem(0);
            EventBus.getDefault().post(new ShopType("6"));
        });
        list_not.setOnClickListener(v -> mPager.setCurrentItem(1));
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        list_yet.setSelected(true);
                        list_not.setSelected(false);
                        break;
                    case 1:
                        list_yet.setSelected(false);
                        list_not.setSelected(true);
                        activity.shop_list.setSelected(true);
                        activity.shop_mima.setSelected(false);
                        activity.shop_wifi.setSelected(false);
                        activity.shop_paper.setSelected(false);
//                        activity.sort.setVisibility(View.INVISIBLE);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected MenDianListPresenter createPresenter() {
        return new MenDianListPresenter((MenDianActivity)getActivity());
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_list;
    }
}
